package com.nondo.dean.product.service;

import com.nondo.dean.common.domain.BaseEntity;
import com.nondo.dean.common.tool.BeanUtils;
import com.nondo.dean.common.tool.RestTemplateUtils;
import com.nondo.dean.common.web.View;
import com.nondo.dean.product.domain.ProductAttach;
import com.nondo.dean.product.domain.ProductAttachExample;
import com.nondo.dean.product.mapper.ProductAttachMapper;
import com.nondo.dean.product.model.AttachBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ken.
 * @version 1.0
 * @email 695093513@qq.com
 * @date 2018/4/8
 */
@Component
public class ProductAttachService {
    @Autowired
    ProductAttachMapper productAttachMapper;

    @Autowired
    RestTemplate restTemplate;

    /**
     * 获取附件列表
     *
     * @param productId
     * @return
     */
    public List<ProductAttach> getList(int productId) {
        ProductAttachExample example = new ProductAttachExample();
        example.setOrderByClause("order_num asc");
        ProductAttachExample.Criteria criteria = example.createCriteria().andProductIdEqualTo(productId).andDelFlagEqualTo(BaseEntity.DEL_FLAG_NORMAL);
        List<ProductAttach> attachList = this.productAttachMapper.selectByExample(example);

        return attachList;
    }

    /**
     * 获取附件列表的ID
     *
     * @param productId
     * @return
     */
    public List getAttachIdList(int productId) {
        List<ProductAttach> list = this.getList(productId);
        List newList = new ArrayList();
        for (ProductAttach item : list) {
            newList.add(item.getAttachId());
        }
        return newList;
    }

    /**
     * 根据产品ID删除产品所有附件
     *
     * @param productId
     * @return
     */
    public int delete(int productId) {
        ProductAttach productAttach = new ProductAttach();
        productAttach.setDelFlag(BaseEntity.DEL_FLAG_DELETE);

        ProductAttachExample example = new ProductAttachExample();
        example.createCriteria().andProductIdEqualTo(productId).andDelFlagEqualTo(BaseEntity.DEL_FLAG_NORMAL);
        return this.productAttachMapper.updateByExampleSelective(productAttach, example);
    }

    /**
     * 插入新的关联
     *
     * @param productAttach
     * @return
     */
    public int insert(ProductAttach productAttach) {
        return this.productAttachMapper.insert(productAttach);
    }

    /**
     * 存储附件表
     *
     * @param path
     * @return
     */
    public AttachBean insertToAttach(String path) {
        AttachBean attachBean = new AttachBean();

        RestTemplateUtils.reInitMessageConverter(restTemplate);
        View result = restTemplate.postForEntity("http://dean-platform/platform/v1/inner/attach/insert", path, View.class).getBody();
        if (result.getCode() == 20000 && result.getData() != null) {
            BeanUtils.copy(result.getData(), attachBean);
        }

        return attachBean;
    }
}
